###############################################################################
#
# RTSAI - Real-Time Strategy engine for AI research
# Copyright(C) 2006 DIMAp-UFRN, licensed under LGPL
# 
# SCONS construction script
#
###############################################################################

import os, os.path, glob, string


# Common general variables
src_dir    = 'src'     # where the source files are stored
script_dir = 'scripts' # where the scripts files are stored
build_dir  = 'build'   # where the generated .o files are stored
bin_dir    = 'bin'     # where the generated binaries are stored
deploy_dir = 'deploy'  # where package files are deployed

lib_name  = 'rtsai'   # the dinamic library generated
app_name  = 'rtsai'   # the application generated
tolua_exe = 'tolua++' # the tolua++ version name


modules = [
  'core',
  'math',
  'ai',
  'common',
  'view',
  'ai/steer/src',
  'ai/steer/include',
  'ai/steer/include/OpenSteer'
]

### Setting platform dependencies #############################################
##### windows
if os.name == 'nt':
    print ("Setting windows (MinGW) tool")
    batch_ext = '.bat'
    libs = ['lua5.1', 'tolua++5.1', 'opengl32', 'glut32', 'glu32']
    env = Environment (
        platform = 'win32',
        tools    = ['mingw','zip']
    )
    env['CPPDEFINES'] = ['_STDCALL_SUPPORTED', '_WIN32']
##### linux
elif os.name == 'posix':
    print ("Setting linux (g++) tool")
    batch_ext = '.sh'
    libs = ['lua5.1', 'tolua++', 'dl', 'glut', 'GLU', 'GL']
    env = Environment (
        platform  = 'posix',
        tools     = ['default','zip'],
        TARFLAGS  = '-c -z',
        TARSUFFIX = '.tgz'
    )
else:
    print "Your platform is not supported"
    exit(1)

### Setting the compile mode (debug or release) ###############################
mode = ARGUMENTS.get('mode', 'release')  # by default, it's release mode
if mode == 'debug' :
    env['CCFLAGS'] = ['-g', '-W1', '-D_DEBUG']
elif mode == 'release' :
    env['CCFLAGS'] = ['-O2', '-DNDEBUG']
else :
   print "Error: mode expected 'debug' or 'release', found: " + mode
   Exit(1)


### Setting the libraries #####################################################
if not env.GetOption('clean'):
    config = env.Configure()
    for libOk in [config.CheckLib(l) for l in libs]:
        if not libOk:
            print "Missing library"
            Exit(1)
    env = config.Finish()
    env['LIBS'] = libs


###############################################################################
# Create a list with the sources (.cpp and .pkg) from all modules.

env['CPPPATH'] = [os.path.join(src_dir, m) for m in modules]

pkg_file = os.path.join (src_dir, 'core', 'rtsailua')
pkg_source = pkg_file + '.pkg'
pkg_target = pkg_file + '.cpp'
pkg_script = os.path.join ( script_dir, 'rtsai.lua' )

def files_from_module(module):
    cpps = glob.glob(os.path.join (src_dir, module, '*.cpp')) # get *.cpp
    #tmpl = glob.glob(os.path.join (src_dir, module, '*T.cpp')) # get templates
    #for t in tmpl:  # remove templates from the sources (cpps)
    #    cpps.remove(t)
    return cpps

# the source list
sources = Flatten([files_from_module(m) for m in modules])
if not (pkg_target in sources) :
    sources.append ( pkg_target )


###############################################################################
# Automatic creation of (lua -> c++) bind through ToLua++
# When a .pkg file is changed, its respective .cpp file is generated and
# compiled.

toluaBld = Builder(
        action='cd ${SOURCE.dir} && ' + tolua_exe + ' -L virtual_method_hooks.lua -n rtsai -o ../../${TARGET} ${SOURCE.file}',
        suffix = '.cpp', src_suffix='.pkg')

env.Append(BUILDERS = {'ToLua': toluaBld})

tolua = env.ToLua ( pkg_target, pkg_source )
env.Depends ( tolua, pkg_script )




########################################################################
# Creation of the rtsai test application
# Usage: scons or scons rtsai
#
main_src = [src_dir + '/main.cpp'] + sources
main_exe = bin_dir + '/rtsai'
main_app = env.Program ( main_exe, main_src )

env.Alias ( 'rtsai', main_app )

Default ( main_app )


### deploy structure
main_dep = [
    main_app,
    'main' + batch_ext,
    'scripts/main.lua',
    'scripts/my_strategy.lua',
    'scripts/poti.lua'
    ]

if os.name == 'nt':
    env.Command( 'main' + batch_ext, '', 'echo @bin\\rtsai scripts\\main.lua > $TARGET' )
    main_dep.append ('glut32.dll')
    env.Zip ( 'deploy/rtsai.zip', main_dep )
else:
    env.Command( 'main' + batch_ext, '', ['echo bin/rtsai scripts/main.lua > $TARGET','chmod a+x $TARGET'] )
    env.Tar ( 'deploy/rtsai.tgz', main_dep )


########################################################################
# Creation of the cature-the-flag solo application
# Usage: scons capture
#
### sources 
capture_src = [src_dir + '/capture.cpp'] + sources
capture_exe = bin_dir + '/capture'
capture_app = env.Program( capture_exe, capture_src )

env.Alias( 'capture', capture_app )


### deploy structure
capture_dep = [
    capture_app,
    'capture' + batch_ext,
    'scripts/capture.lua',
    'scripts/simple_seeker.lua',
    'scripts/attacker.lua',
    'scripts/poti.lua'
    ]

if os.name == 'nt':
    env.Command( 'capture' + batch_ext, '', 'echo @bin\\capture scripts\\capture.lua > $TARGET' )
    capture_dep.append ('glut32.dll')
    env.Zip ( 'deploy/capture.zip', capture_dep )
else:
    env.Command( 'capture' + batch_ext, '', ['echo bin/capture scripts/capture.lua > $TARGET','chmod a+x $TARGET'] )
    env.Tar ( 'deploy/capture.tgz', capture_dep )


########################################################################
# Creation of the full cature-the-flag application
# Usage: scons capture
#
### sources 
fcapture_src = [src_dir + '/fcapture.cpp'] + sources
fcapture_exe = bin_dir + '/fcapture'
fcapture_app = env.Program( fcapture_exe, fcapture_src )

env.Alias( 'fcapture', fcapture_app )


### deploy structure
fcapture_dep = [
    fcapture_app,
    'fcapture' + batch_ext,
    'scripts/fcapture.lua',
    'scripts/ab_strategy.lua',
    'scripts/cd_strategy.lua',
    'scripts/poti.lua',
    'doc/rtsai.html'
    ]

if os.name == 'nt':
    env.Command( 'fcapture' + batch_ext, '', 'echo @bin\\fcapture scripts\\fcapture.lua > $TARGET' )
    fcapture_dep.append ('glut32.dll')
    env.Zip ( 'deploy/fcapture.zip', fcapture_dep )
else:
    env.Command( 'fcapture' + batch_ext, '', ['echo bin/fcapture scripts/fcapture.lua > $TARGET','chmod a+x $TARGET'] )
    env.Tar ( 'deploy/fcapture.tgz', fcapture_dep )




########################################################################
# Creation of automatic documentation
# Usage: scons doc
#
env.Command('doc/*', 'doxys.conf', 'doxys doxys.conf')


########################################################################
# Unit testing
# Usage: scons test
#
#def test_emitter(target, source, env):
#    target = [ t.path + 'Test.cpp' for t in target ]
#    source = [ s.path + 'Test.h'   for s in source ]
#    return target, source

#cpp_builder = env['BUILDERS']['Program'].src_builder

#unit_test_bld = Builder(
#        action = 'cd ${SOURCE.dir} && cxxtestgen.py -o ${TARGET.file} --error-printer ${SOURCE.file}',
#        emitter=test_emitter)

#env.Append(BUILDERS = {'Test': unit_test_bld})

#env.Test('src/common/test/Pool')
#env.Alias('test', env.Program('bin/test/PoolTest', 'src/common/test/PoolTest.cpp'))
#env.Alias('test', env.Test('src/common/test/Pool'))
